package com.matrix.java.chap_04;

import java.util.Scanner;

/*
    十六进制转二进制
 */
public class Ex4_12 {
    public static void main(String[] args) {
        Scanner input = new Scanner(System.in);
        System.out.print("Enter a hex digit: ");
        String hex = input.next();
        char ch = Character.toUpperCase(hex.charAt(0));
        if (ch <= 'F' && ch >= 'A') {
            int value = ch - 'A' + 10;
            System.out.println("The binary value is " + (value / 8) % 2 + (value / 4) % 2 + (value / 2) % 2 + value % 2);
        } else if (Character.isDigit(ch)) {
            System.out.println("The binary value is " + (ch / 8) % 2 + (ch / 4) % 2 + (ch / 2) % 2 + ch % 2);
        } else {
            System.out.println(ch + " is an invalid input");
            System.out.println();
            input.close();
        }
    }
}
